package com.hengda.museumonline.ui;

import android.content.Intent;
import android.os.BaseBundle;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.SectionedExpandable.Item;
import com.hengda.museumonline.SectionedExpandable.ItemClickListener;
import com.hengda.museumonline.SectionedExpandable.Section;
import com.hengda.museumonline.SectionedExpandable.SectionedExpandableLayoutHelper;
import com.hengda.museumonline.adapter.RelationNewsAdapter;
import com.hengda.museumonline.entity.BaseBean;
import com.hengda.museumonline.entity.CategoryEvent;
import com.hengda.museumonline.entity.NewsBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.rxbus.RxBus;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.ArrayList;

/**
 * @author：JFWU on 2016/10/18 20:05
 * @E-mail：jeffy12138@126.com
 */
public class NewsContentActivity extends BaseActivity implements View.OnClickListener {
    private int object_id;
    private NewsBean newsBean;
    private TextView posttitle;
    private TextView postmodified;
    private TextView postsource;
    private WebView postcontent;
    static final String mimeType = "text/html";
    static final String encoding = "utf-8";
    private android.support.v7.widget.RecyclerView relationNews;
    private android.widget.ImageView mainreturn;
    private android.widget.ImageView newscomment;
    private android.widget.ImageView newscollection;
    private android.widget.ImageView newshits;
    private android.widget.ImageView newsfout;
    private PopupWindow mPopWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        this.newsfout = (ImageView) findViewById(R.id.news_fout);
        newsfout.setOnClickListener(this);
        this.newshits = (ImageView) findViewById(R.id.news_hits);
        newshits.setOnClickListener(this);
        this.newscollection = (ImageView) findViewById(R.id.news_collection);
        newscollection.setOnClickListener(this);
        this.newscomment = (ImageView) findViewById(R.id.news_comment);
        newscomment.setOnClickListener(this);
        this.mainreturn = (ImageView) findViewById(R.id.main_return);
        mainreturn.setOnClickListener(this);
        this.relationNews = (RecyclerView) findViewById(R.id.relationNews);
        relationNews.setLayoutManager(new LinearLayoutManager(this));
        this.postcontent = (WebView) findViewById(R.id.post_content);
        postcontent.setBackgroundColor(0); // 设置背景色
        postcontent.getBackground().setAlpha(0); // 设置填充透明度 范围：0-255
        // postcontent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // postcontent.getSettings().setUseWideViewPort(true);
        //  postcontent.getSettings().setLoadWithOverviewMode(true);
        postcontent.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
        this.postsource = (TextView) findViewById(R.id.post_source);
        this.postmodified = (TextView) findViewById(R.id.post_modified);
        this.posttitle = (TextView) findViewById(R.id.post_title);
        object_id = Integer.parseInt(getIntent().getStringExtra("object_id"));
        getContentfromNet();
        System.out.println("object_id:" + object_id);
    }

    private void getContentfromNet() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "news/content", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("object_id", object_id);

        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    newsBean = gson.fromJson(response.get().toString(), NewsBean.class);
                    if (newsBean != null && newsBean.getData() != null) {
                        setData(newsBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
                dialog.dismiss();
            }
        });
    }

    private void setData(NewsBean newsBean) {
        posttitle.setText(newsBean.getData().getPost_title());
        postmodified.setText(newsBean.getData().getPost_modified());
        postsource.setText(newsBean.getData().getPost_source());
        postcontent.loadDataWithBaseURL(null, newsBean.getData().getPost_content(), mimeType, encoding, null);
        if (newsBean.getData().getRelationNews() != null && newsBean.getData().getRelationNews().size() > 0) {
            relationNews.setAdapter(new RelationNewsAdapter(NewsContentActivity.this, newsBean));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.news_comment:
                Intent intent = new Intent(NewsContentActivity.this, NewsCommentActivity.class);
                intent.putExtra("id", object_id);
                startActivity(intent);
                break;
            case R.id.news_collection:
                dofavorite();
                break;
            case R.id.news_hits:
                like();
                break;
            case R.id.news_fout:
                setFout();
                break;
        }
    }

    private void setFout() {
        View contentView = LayoutInflater.from(NewsContentActivity.this).inflate(R.layout.setfout_pop, null);
        RadioGroup rg = (RadioGroup) contentView.findViewById(R.id.rg);
        final RadioButton rb1 = (RadioButton) contentView.findViewById(R.id.rb1);
        final RadioButton rb2 = (RadioButton) contentView.findViewById(R.id.rb2);
        final RadioButton rb3 = (RadioButton) contentView.findViewById(R.id.rb3);
        final RadioButton rb4 = (RadioButton) contentView.findViewById(R.id.rb4);
        ImageView close = (ImageView) contentView.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindow.dismiss();
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                setRB(rb1);
                setRB(rb2);
                setRB(rb3);
                setRB(rb4);
                switch (i) {
                    case R.id.rb1:


                        break;
                    case R.id.rb2:

                        break;
                    case R.id.rb3:

                        break;
                    case R.id.rb4:

                        break;
                }
            }
        });
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        mPopWindow.showAtLocation(newsfout, Gravity.BOTTOM, 0, 0);
    }

    private void setRB(RadioButton rb) {
        if (rb.isChecked()) {
            rb.setBackgroundResource(R.drawable.news_size);
            rb.setTextColor(getResources().getColor(R.color.bg));
        } else {
            rb.setBackgroundResource(0);
            rb.setTextColor(getResources().getColor(R.color.comment));
        }
    }

    private void like() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "user/like", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("token", spUtil.getToken());
        request.add("type", 0);
        request.add("id", object_id);
        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    BaseBean baseBean = new Gson().fromJson(response.get().toString(), BaseBean.class);
                    getToast(baseBean.getMsg());
                } else {
                    getToast("点赞失败");
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("点赞失败");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void dofavorite() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "user/dofavorite", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("token", spUtil.getToken());
        request.add("type", 0);
        request.add("id", object_id);
        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    BaseBean baseBean = new Gson().fromJson(response.get().toString(), BaseBean.class);
                    getToast(baseBean.getMsg());
                } else {
                    getToast("收藏失败");
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("收藏失败");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }
}
